<?php
namespace app\api\model;

use think\Model;
use think\Request;

class Client extends Model{
	//获得访客浏览器类型
	public function get_browser() {
	    if(empty($_SERVER['HTTP_USER_AGENT'])){
		    return 'robot！';
		}
		if((false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))&&(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE)){
		    return 'Internet Explorer 11.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
		    return 'Internet Explorer 10.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
		    return 'Internet Explorer 9.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
		    return 'Internet Explorer 8.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
		    return 'Internet Explorer 7.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
		    return 'Internet Explorer 6.0';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
		    return 'Edge';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
		    return 'Firefox';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
		    return 'Chrome';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
		    return 'Safari';
		}
	    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
		    return 'Opera';
		}
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
		    return '360SE';
		}
		//微信浏览器
		if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
		    return 'MicroMessage';
		}
		return "unknow";
	}

	//获得访客浏览器语言
	public function get_lang(){
		if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
			$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
			$lang = substr($lang,0,5);
			if(preg_match("/zh-cn/i",$lang)){
                $lang = "简体中文";
	        }elseif(preg_match("/zh/i",$lang)){
	            $lang = "繁体中文";
	        }else{
	            $lang = "English";
	        }
            return $lang;
		}else{
			return "unknow";
		}
	}

	//获取访客操作系统
	public function get_os(){
		if(!empty($_SERVER['HTTP_USER_AGENT'])){
			$OS = $_SERVER['HTTP_USER_AGENT'];
			if(preg_match('/win/i',$OS)){
				$OS = 'Windows';
			}elseif(preg_match('/mac/i',$OS)){
				$OS = 'MAC';
			}elseif(preg_match('/linux/i',$OS)){
				$OS = 'Linux';
			}elseif(preg_match('/unix/i',$OS)){
				$OS = 'Unix';
			}elseif(preg_match('/bsd/i',$OS)){
				$OS = 'BSD';
			}else{
				$OS = 'Other';
			}
			return $OS;  
		}else{
			return "unknow";
		}
	}

	//根据ip信息
	public function get_ip_from($ip="myip"){
		$ip_json=@file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//根据taobao ip
		$ip_arr=json_decode($ip_json,true);
		if($ip_arr['code']==0){
			return $ip_arr['data'];
		}else{
			return false;
		}
	}
}
?>